<template>
    
    <!-- 组件的模板 -->
    <div class="weui-cells__title bg_color" v-if="desc">{{desc}}</div>
    <div style="height:12px;background-color:#f2f2f2;" v-else></div>
    <div id="items">
        <div v-for="item in reportInfo.info">
            
            <div class='border-top' v-if="item.type === 'Date'">
                <formdate
                            :title="item.info.title" 
                            :tip="item.info.tip" 
                            :date.sync="item.info.value"
                            :required="item.info.required"
                            :isvalid.sync="item.isValid"
                ></formdate>
            </div>
            <div class='border-top' v-if="item.type === 'SingleText'">
                <formsingle 
                            :title="item.info.title" 
                            :tip="item.info.tip" 
                            :val.sync="item.info.value"
                            :required="item.info.required"
                            :isvalid.sync="item.isValid"
                ></formsingle>
            </div>
            <div class='border-top' v-if="item.type === 'TextArea'">
                <formtextarea 
                            :title="item.info.title" 
                            :tip="item.info.tip" 
                            :val.sync="item.info.value"
                            :required="item.info.required"
                            :isvalid.sync="item.isValid"
                ></formtextarea>
            </div>
            <div class='border-top' v-if="item.type === 'Number'">
                <formnumber
                            :title="item.info.title"
                            :tip="item.info.tip"
                            :val.sync="item.info.value"
                            :required="item.info.required"
                            :isvalid.sync="item.isValid"
                ></formnumber>
            </div>
            <div class='border-top' v-if="item.type === 'Select'">
                <formselect 
                            :title="item.info.title" 
                            :tip="item.info.tip" 
                            :val.sync="item.info.value"
                            :opts="item.info.options"
                            :required="item.info.required"
                            :isvalid.sync="item.isValid"
                ></formselect>
            </div>
            <div class='border-top' v-if="item.type === 'Location'">
                <div class="comp-wrap">
                    <div class="weui-cells__title black">{{item.info.title}}<span v-if="item.info.required == 1" class="required_icon"> (必填)</span></div>
                    <div class="weui-cells__tips">{{item.info.tip}}</div>
                    <!-- <location></location> -->
                    <div class="weui-cells">
                        <div class="weui-cell padding">
                            <div class="weui-cell__bd isValue">
                                <p v-if="!item.info.value.addr" class="isTip">点击定位</p>
                                <p v-else>{{item.info.value.addr}}</p>
                            </div>
                            <div class="weui-cell__ft">
                            <a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default padding-12" @click="getLocation($index)">{{!item.info.value.addr ? '定位' : '重新定位'}}</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class='border-top' v-if="item.type === 'Imageview'">
                <div class="comp-wrap">
                    <!-- <imageview :title="item.info.title"></imageview> -->
                    <div class="weui-cells weui-cells_form left_0">
                        <div class="weui-cell" style="padding:0px;">
                            <div class="weui-cell__bd">
                                <div class="weui-uploader">
                                    <div class="bg_f2">
                                        <div class="weui-uploader__hd pad-lr black">
                                            <p class="weui-uploader__title isTitle">{{item.info.title}}<span v-if="item.info.required == 1" class="required_icon"> (必填)</span></p>
                                            
                                        </div>
                                        <div class="weui-cells__tips pad-left-0 pad-lr">{{item.info.tip}}</div>
                                    </div>
                                    <div class="weui-uploader__bd pad-lr pad-tb" style="position:relative;">
                                        <ul class="weui-uploader__files" id="uploaderFiles">
                                            <li class="weui-uploader__file img" 
                                                v-for="imgurl in item.info.thumb_image_value" track-by="$index"
                                                :style="'background-image:url('+imgurl+')'"
                                                @click="pictureOperationDialog($parent.$index, $index)"
                                                >
                                                <!-- <img :src="imgurl" alt="" @click="pictureOperationDialog($parent.$index, $index)"> -->
                                            </li>
                                        </ul>
                                        <div class="weui-uploader__input-box">
                                            <input id="uploaderInput" class="weui-uploader__input" type="" readonly @click="selectImage($index)" />
                                        </div>
                                        <div class="weui-uploader__info img_cont">{{item.info.value.length}}/20</div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import widget from 'gapi/widget.js'
    import clientUi from 'gapi/clientUi.js'
    import * as util from 'js/util.js'
    import commonPage from 'clientApi/commonPage.js'
    import location from 'gapi/location.js'
    import cache from 'gapi/cache.js'

    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */
    import Formsingle from 'components/Formsingle/Formsingle'
    import Formtextarea from 'components/Formtextarea/Formtextarea'
    import Formnumber from 'components/Formnumber/Formnumber'
    import Formselect from 'components/Formselect/Formselect'
    import Formdate from 'components/Formdate/Formdate'
    import Location from 'components/Location/Location'
    import { getLoc } from 'components/Location/LocationGetters'
    import Imageview from 'components/Imageview/Imageview'
    import { getImageArray } from 'components/Imageview/ImageviewGetters'

    /**
     *  组件实例对象
     */
    export default {
        components: {
            Formsingle,
            Formtextarea,
            Formnumber,
            Formselect,
            Formdate,
            Location,
            Imageview
        },
        data: function(){
            return{
                reportInfo: {
                    team_id: 0,
                    project_id: 0,
                    form_id: 0,
                    info: []
                },
                par_index: 0,
                desc: '',
                isTip: true
            }
        },
        vuex: {
            getters: {
                getLoc,
                getImageArray
            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                let temp_info_obj = JSON.parse(util.getUrddataString('temp_info'));
                temp_info_obj.format_info.forEach((obj) => {
                    if(obj.type === 'Imageview'){
                        obj.info.value = []
                        obj.info.thumb_image_value = []
                    }
                })
                // alert(document.title)
                this.reportInfo.info = temp_info_obj.format_info;
                this.desc = temp_info_obj.desc;
                document.title = temp_info_obj.title || '表单模板预览';
                let isShowUse = temp_info_obj.isShowUse;
                if(isShowUse == 1){
                    commonPage.updateTitleBar({
                        //backAction: 'checkChange',    // 点击返回按钮的动作（即回调函数名称）
                        // leftDisplay: false,            // 是否显示左按钮
                        // leftText: '',                // 左按钮文案
                        //leftAction: 'checkChange',    // 点击左按钮的动作（即回调函数名称）
                        rightDisplay: true,            // 是否显示右按钮
                        rightText: '使用',            // 右按钮文案
                        rightAction: 'saveResume'    // 点击右按钮的文案（即回调函数名称）
                    })
                    window.saveResume = () => {
                        let temp_info = util.getUrddataString('temp_info')
                        let encodeStr = encodeURIComponent(`temp_info=${temp_info}&page_type=formTempPreview`)
                        window.location.href = `dmsaas://EditForm?urdData=${encodeStr}`
                    }
                }else if(isShowUse == '-5'){
                    commonPage.updateTitleBar({
                        rightDisplay: true,            // 是否显示右按钮
                        rightText: '删除',            // 右按钮文案
                        rightAction: 'removeResume'    // 点击右按钮的文案（即回调函数名称）
                    });
                    let form_id = temp_info_obj.form_id;
                    window.removeResume = () => {
                        clientUi.showDialog('提醒', '确认删除此表单？', (status) => {
                            // alert(status.status)
                            if(status.status == 1){
                                cache.getCache('user_info', (data) => {
                                    util.ajax({
                                        url: '/form/status',
                                        type: 'POST',
                                        data: {
                                            team_id: data.team_id,
                                            project_id: data.project_id,
                                            form_id: form_id,
                                            status:-5
                                        },
                                        timeout: 10000,
                                        success: (data) => {
                                            // success
                                            // alert(JSON.stringify(data))
                                            if(data.errno == 0){
                                                widget.toast('删除成功')
                                                commonPage.goBack(1)
                                            }else if(data.errno == 22005){
                                                widget.toast('该表单不可删除')
                                            }else{
                                                // widget.toast('服务器异常')
                                                widget.toast(data.errmsg)
                                                // alert(JSON.stringify(data))
                                            }
                                        },
                                        error: (xhr, status) => {
                                            // error
                                            widget.toast('服务器异常')
                                        },
                                        noNetwork: () => {
                                            // 无网络的回调
                                            widget.toast('网络连接失败，请检查网络')
                                        }
                                    })
                                })
                            }
                        }, '确认', '取消')
                    }
                }
            },
            getLocation (index){
                commonPage.loadPageStatus('loading')
                util.requestPOI((data) => {
                    if(data.address){
                        this.reportInfo.info[index].info.value.lat = data.lat
                        this.reportInfo.info[index].info.value.lng = data.lon
                        this.reportInfo.info[index].info.value.addr = data.address
                        // alert(this.reportInfo.info[index].info.value.addr)
                        // widget.toast('定位成功')
                        this.isTip = false;
                    }else{
                        this.isTip = true;
                        widget.toast(data.error_msg)
                    }
                    commonPage.loadPageStatus('loadSuccess')
                })
            },
            selectImage (index){
                // alert(JSON.stringify(this.reportInfo.info[index]))
                if(this.reportInfo.info[index].info.value.length < 20){
                    if(this.reportInfo.info[index].info.isSelectImg == 1){
                        clientUi.selectAndUploadImage((data) => {
                            // alert(util.baseImageDomain + data.imageURL)
                            if(data.status == 1){
                                this.reportInfo.info[index].info.value.push(util.baseImageDomain + data.imageURL)
                                this.reportInfo.info[index].info.thumb_image_value.push(util.baseImageDomain + data.imageURL)
                            }
                        })
                    }else{
                        clientUi.invokeCamera((data) => {
                            if(data.status == 1){
                                this.reportInfo.info[index].info.value.push(util.baseImageDomain + data.imageURL)
                                this.reportInfo.info[index].info.thumb_image_value.push(util.baseImageDomain + data.imageURL)
                            }
                            // alert(JSON.stringify(this.reportInfo.info[index].info.value))
                        })
                    }
                }else{
                    widget.toast('已达上传上限')
                }
                
            },
            pictureOperationDialog (par_index, index){
                // alert(par_index)
                // alert(index)
                clientUi.pictureOperationDialog('title', this.reportInfo.info[par_index].info.value, index, () => {
                    this.reportInfo.info[par_index].info.value.splice(index, 1)
                    // alert(JSON.stringify(this.reportInfo.info[par_index].info.value))
                })
            }
        },
        created(){
            this.init();
            
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style type="text/css">
    html {
        -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    }
    html body{
        /*background-color: #fff;*/
        background-color: #f2f2f2;
    }
    div.weui-cells{
        margin-top:0px;
        font-size: 15px;
        
    }
    div.weui-cell{
        padding-top: 14px;
        padding-bottom: 14px;
    }
    div.comp-wrap{
        background-color: #f2f2f2;
        /*background-color: #fff;*/
        padding-top: 0.1px;
    }
    div.border-top{
        /*border-top:12px solid #f2f2f2;*/
        /*position: relative;*/
        /*padding:0.1px;*/
    }
    #items div:first-child div.border-top{
        border-top: 0;
    }
    div.black{
        color: rgb(51,51,51);
        font-size: 16px;
        margin-bottom: 0px;
        margin-top: 16px;
        line-height: 1.375;
    }
    div.border-top div.weui-cells__tips{
        margin-bottom: 6px;
        color: rgb(102,102,102);
        font-size: 14px;
        margin-top: 0;
        line-height: 1.375;
    }
    li.img{
        overflow: hidden;
        width:60px;
        height:60px;
        /*box-sizing: border-box;
        margin-right:0;
        padding-right: 9px;*/
        margin-right: 8px;
        margin-bottom: 8px;
    }
    li.img img{
        /*width:100%;*/
        width: 60px;
        /*height: 60px;*/
    }
    div.weui-uploader__input-box{
        width:60px;
        height:60px;
        margin-right: 8px;
        margin-bottom: 8px;
    }
    div.pad-left-0{
        padding-left: 0;
    }
    div.pad-lr{
        padding:0px 12px;
    }
    div.pad-tb{
        padding:24px 12px 32px;
    }
    div.weui-uploader__bd{
        margin:0;
    }
    div.bg_color{
        background-color: #f2f2f2;
        margin:0;
        /*margin-top:7px;*/
        /*padding-top: 18px;
        padding-bottom: 4px;*/
        padding:10px 12px;
        line-height: 1.3;

        text-align: center;
    }
    div.weui-textarea-counter,div.weui-uploader__info{
        font-size: 12px;
        color: #999;
    }
    div.weui-cells:before{
        border-top: 1px solid #e5e5e5;
        /*left:12px;*/
        left:0px;
    }
    div.weui-cells.left_0:before{
        left:0;
    }
    div.weui-cells:after{
        border-bottom: 1px solid #e5e5e5;
    }
    div.weui-cell a.weui-btn_default{
        background-color: #fff;
        border:none;
        color:#4499ff;
        padding-right: 4px;
    }
    div.weui-cell a.weui-btn_default:active{
        background-color: #fff;
    }
    .padding-12{
        padding-left: 12px;
        font-size: 14px;
    }
    .isTip{
        color: #ccc;
    }
    p.weui-uploader__title{
        color:rgb(51,51,51);
    }
    p.isTitle{
        font-size: 16px;
    }
    input.weui-input{
        font-size: 16px;
    }
    
    input{
        -webkit-appearance: none;
    }
    textarea.weui-textarea{
        font-size: 16px;
    }
    div.isValue p{
        font-size: 16px;
    }
    div.bg_f2{
        background-color: #f2f2f2;
        padding:0.1px;
    }
    div.img_cont{
        position: absolute;
        right:12px;
        bottom:12px;
        color: rgb(153,153,153);
        font-size: 14px;
    }
    span.required_icon{
        /*display: inline-block;
        width:7px;
        height:7px;
        margin-left: 5px;
        margin-bottom: 2px;
        background-image: url(../../static/imgs/required.png);*/
        font-size: 14px;
        color: rgb(153,153,153);
    }
</style>


